package com.gwz.bus.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gwz.bus.domain.Goods;
import com.gwz.bus.domain.Inport;
import com.gwz.bus.mapper.GoodsMapper;
import com.gwz.bus.mapper.InportMapper;
import com.gwz.bus.service.InportService;
import com.gwz.bus.vo.InportVo;
import com.gwz.sys.utils.DataGridView;

@Service
@Transactional
public class InportServiceImpl implements InportService {

	@Autowired
	private InportMapper inportMapper;

	@Autowired
	private GoodsMapper goodsMapper;

	@Override
	public DataGridView queryAllInports(InportVo inportVo) {
		Page<Object> page = PageHelper.startPage(inportVo.getPage(), inportVo.getLimit());
		List<Inport> data = this.inportMapper.queryAllInport(inportVo);
		DataGridView view = new DataGridView(page.getTotal(), data);
		return view;
	}

	/**
	 * 添加进货
	 */
	@Override
	public void addInport(InportVo inportVo) {
		// 添加进货数据
		this.inportMapper.insertSelective(inportVo);
		// 增加商品库存
		Goods record = this.goodsMapper.selectByPrimaryKey(inportVo.getGoodsid());
		record.setNumber(record.getNumber() + inportVo.getNumber());// 库存=之前的数量+进货的数量
		this.goodsMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public Inport queryInportById(Integer id) {

		return this.inportMapper.selectByPrimaryKey(id);
	}

	@Override
	public void updateInport(InportVo inportVo) {
		this.inportMapper.updateByPrimaryKeySelective(inportVo);
	}

	@Override
	public void deleteInport(Integer id) {
		Inport inport = inportMapper.selectByPrimaryKey(id);
		// 增加商品库存
		Goods record = this.goodsMapper.selectByPrimaryKey(inport.getGoodsid());
		record.setNumber(record.getNumber() - inport.getNumber());// 删除，库存=之前的数量-进货的数量
		this.goodsMapper.updateByPrimaryKeySelective(record);
		//删除进货信息
		this.inportMapper.deleteByPrimaryKey(inport.getId());
	}

}
